package com.granada.model.pc;

import com.github.pagehelper.PageHelper;
import com.granada.entity.*;
import com.granada.utils.PageUtils;
import com.granada.entity.*;
import com.granada.model.common.User;
import com.granada.utils.MapperBeanUtils;
import com.granada.utils.PasswordUtils;
import com.granada.vo.resp.PageVO;

import java.util.Date;
import java.util.List;

public class UserWithPing<
        T extends SysUser,
        EW extends SysUserWechatRelation,
        W extends SysUserWechat,
        F extends Page
        > extends User<T,EW,W,F> {


    public UserWithPing(T vo, EW ewo, W wo, F fo, Ao ao) {
        super(vo, ewo, wo, fo, ao);
    }


    @Override
    public PageVO<T> getList() throws Exception {
        PageHelper.startPage(fo.getPageNum(),fo.getPageSize());
        List<T> sysUsers = (List<T>) MapperBeanUtils.getSysUserMapper().selectByPingUser(vo);
        return PageUtils.getPageVO(sysUsers);
    }

    @Override
    public Long addSysUserBefore() {
        String salt = PasswordUtils.getSalt();
        SysUser sysUser = SysUser.builder()
                .id(vo.getId())
                .username(vo.getUsername())
                .phone(vo.getPhone())
                .sex(vo.getSex())
                .email(vo.getEmail())
                .realName(vo.getRealName())
                .nickName(vo.getNickName())
                .salt(salt)
                .password(PasswordUtils.encode(vo.getPassword(), salt))
                .status(1)
                .updateTime(new Date())
                .build();
        Long userId = MapperBeanUtils.getSysUserMapper().insert(sysUser);
        return userId;
    }

    @Override
    public void addSysUserAfter() {

    }

    @Override
    public Long addSysUserWechatBefore() throws Exception {
        return null;
    }

    @Override
    public void addSysUserWechatAfter() throws Exception {

    }

    @Override
    public Long addSysUserWechatRelationBefore() throws Exception {
        return null;
    }

    @Override
    public void addSysUserWechatRelationAfter() throws Exception {

    }

    @Override
    public void updateSysUserBefore() throws Exception {
        String salt = PasswordUtils.getSalt();
        SysUser sysUser = SysUser.builder()
                .id(vo.getId())
                .username(vo.getUsername())
                .phone(vo.getPhone())
                .sex(vo.getSex())
                .email(vo.getEmail())
                .realName(vo.getRealName())
                .nickName(vo.getNickName())
                .salt(salt)
                .password(PasswordUtils.encode(vo.getPassword(), salt))
                .status(1)
                .updateTime(new Date())
                .build();
        MapperBeanUtils.getSysUserMapper().updateByPrimaryKeySelective(sysUser);

    }

    @Override
    public void updateSysUserAfter() throws Exception {

    }


    @Override
    public void updateSysUserWechatBefore() throws Exception {

    }

    @Override
    public void updateSysUserWechatAfter() throws Exception {

    }

    @Override
    public void updateSysUserWechatRelationBefore() throws Exception {

    }

    @Override
    public void updateSysUserWechatRelationAfter() throws Exception {

    }
}
